package com.dx.as.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import com.dx.as.ToolBarOptions;
import com.dx.as.UI;
import com.dx.as.common.log.LogUtil;

public abstract class TFragment extends Fragment {
    private int containerId;
    private int layoutId;
    private final static String TAG = "fragment";
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layoutId = setLayoutI();
        initRpo();
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onCreate : "+hashCode());
    }
    public int getContainerId() {
        return containerId;
    }

    public void setContainerId(int containerId) {
        this.containerId = containerId;
    }

    @Override
    public void onPause() {
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onPause : "+hashCode());
        super.onPause();
    }

    @Override
    public void onResume() {
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onResume : "+hashCode());
        super.onResume();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layoutId,container,false);
        setToolbar();
        optionAsCreateView();
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onCreateView : "+hashCode());
        return view;
    }

    public void optionAsCreateView(){

    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        dataOpion();
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onActivityCreated : "+hashCode());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            setToolbar();
        }
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onHiddenChanged : "+hashCode());
    }

    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(TAG,this.getClass().getSimpleName()+"  :  onDestroy : "+hashCode());
    }

    protected void showKeyboard(boolean isShow) {
        Activity activity = getActivity();
        if (activity == null) {
            return;
        }

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }

        if (isShow) {
            if (activity.getCurrentFocus() == null) {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            } else {
                imm.showSoftInput(activity.getCurrentFocus(), 0);
            }
        } else {
            if (activity.getCurrentFocus() != null) {
                imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }

        }
    }

    protected void hideKeyboard(View view) {
        Activity activity = getActivity();
        if (activity == null) {
            return;
        }

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }

        imm.hideSoftInputFromWindow(
                view.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }

    protected <T extends View> T findView(int resId) {
        return (T) (getView().findViewById(resId));
    }

    protected void setToolBar(ToolBarOptions toolBarOptions) {
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).setToolbar(toolBarOptions);
        }
    }

    public void goToNewActivity(Class<?> clas){
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).goToNewActivity(clas);
        }
    }

    public void goToNewActivity(Class<?> cls,Bundle bundle){
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).goToNewActivity(cls,bundle);
        }
    }

    public void goToNewActivityForResult(Class<?> cls,int code){
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).goToNewActivityForResult(cls,code);
        }
    }

    public void goToNewActivityForResult(Class<?> cls,int code, Bundle bundle){
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).goToNewActivityForResult(cls,code,bundle);
        }
    }


    protected void hideToolbar(boolean hide) {
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).hideToolbar(hide);
        }
    }

    protected void setToolbar(){

    }

    protected void showSnackBar(String msg){
        if (getActivity() != null && getActivity() instanceof UI) {
            ((UI)getActivity()).showSnackBar(msg);
        }
    }

    /**
     * 设置layoutId,申明控件
     */
    public abstract void initView();

    /**
     *设置fragment layoutId
     */
    public abstract int  setLayoutI();


    public abstract void  dataOpion();

    public abstract void initRpo();
}
